// web/src/app/api/(social)/members/route.ts
import {NextRequest, NextResponse} from "next/server";
import {db} from "@/lib/db";
import {currentUser} from "@/lib/auth";

export async function GET(req: NextRequest) {
    const searchParams = req.nextUrl.searchParams;
    let workspaceId = (searchParams.get('workspaceId'))
    let userId = (searchParams.get('userId'))

    let where = {}
    // @ts-ignore
    if (workspaceId) where.workspaceId = workspaceId
    // @ts-ignore
    if (userId) where.userId = userId
    const members = await db.member.findMany({
        where,
        include: {
            user: true
        }
    })

    return NextResponse.json(members)
}

export async function POST(req: NextRequest) {
    try {
        const data = await req.json()
        const user = await currentUser()

        const member = await db.member.create({
            data: {
                ...data,
                userId: user?.id!
            }
        })
        return NextResponse.json({member})
    } catch (err) {
        console.error(err)
        return NextResponse.json({err})
    }
}